
# Description ------------------------------------------------------------------
#   Convert older 'rds' files to 'fst' for faster loading

# Setup ------------------------------------------------------------------------
# Load 'fst' package
pacman::p_load(fst, stringr)
# Define directories
dir_project = 'T:/Home/Edward/Elasticity'
dir_rds     = file.path(dir_project, 'DataR')

# Convert files ----------------------------------------------------------------
# Files of interest
files = c(
  'zipsNeighbor0.rds',
  'zipsNeighbor1.rds',
  'zipsNeighbor2.rds',
  'zipsNeighbor3.rds',
  'commonCitiesZipsBills.rds'
)
# Re-save files as fst
lapply(
  X = files,
  FUN = function(f) {
    # Load the file
    tmp = readRDS(file.path(dir_rds, 'Prices/Joint/', f))    
    # Save as fst
    write_fst(
      x = tmp,
      path = file.path(
        dir_rds, 'Prices/Joint/',
        f %>% str_replace('\\.rds', '\\.fst')
      ),
      compress = 75
    )
    rm(tmp); invisible(gc())
    # Return
    return('success!')
  }
)


